<?php

namespace App\Http\Resources;

use App\Enums\VersionType;
use App\Models\Message;
use App\Models\User;
use Illuminate\Http\Request;
use Illuminate\Http\Resources\Json\JsonResource;
use Illuminate\Support\Facades\Storage;
use Illuminate\Support\Str;

class UserResource extends JsonResource
{
    /**
     * @mixin User
     * @param Request $request
     * @return array
     */
    public function toArray($request): array
    {
        return [
            'id' => $this->id,
            'nickname' => $this->nickname,
            'phone' => $this->phone ? Str::replace(Str::substr($this->phone, 3, 4), '****', $this->phone) : '',
            'avatar' => $this->avatar ? Storage::disk('public')->url($this->avatar) : asset('/images/default-avatar.png'),
            'unread_message' => Message::query()->where('user_id',$this->id)->where('read',false)->count(),
            'created_at' => $this->created_at->toDateTimeString()
        ];
    }
}
